Component({
  options: {
    addGlobalClass: true, // 使用全局样式
    pureDataPattern: /^_/ // 纯数据字段
  },
  properties: {
    itemsList: {
      type: Object,
      value: []
    }
  },
  data: {
    enlargeIndexes: [],
    multiple: 0,
    currentIndex: 1
  },
  observers: {
    // 监听单个数据字段
    'itemsList': function (e) {
      let multiple = e.length >= 3 ? 3 : e.length;
      this.setData({
        multiple: multiple
      })
    }
  },
  lifetimes: {
    attached() {
      console.log('组件实例进入页面节点树时触发');
      this.updateEnlargeIndexes(1); // 初始化轮播图
    },
    detached() {
      console.log('组件实例被移除时触发');
    }
  },
  methods: {
    swiperChange: function (e) {
      let current = e.detail.current;
      const {
        itemsList
      } = this.data;
      this.setData({
        currentIndex: current
      });
      if (current + 1 == itemsList.length) {
        this.updateEnlargeIndexes(0);
        return;
      }
      this.updateEnlargeIndexes(current + 1);
    },
    updateEnlargeIndexes: function (current) {
      const {
        itemsList
      } = this.data;
      const length = itemsList.length;
      const newEnlargeIndexes = [];

      // 计算需要放大的索引（当前项及左右两项）
      const prevIndex = (current - 1 + length) % length;
      const nextIndex = (current + 1) % length;

      newEnlargeIndexes[current] = true;
      newEnlargeIndexes[prevIndex] = true;
      newEnlargeIndexes[nextIndex] = true;

      this.setData({
        enlargeIndexes: newEnlargeIndexes
      });
    }
  }
})